package com.nba.system.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.nba.common.exception.CustomException;
import com.nba.common.cache.GlobalCache;
import com.nba.common.controller.BaseController;
import com.nba.common.dao.IJdbcDao;
import com.nba.common.framework.ErrorCode;
import com.nba.common.form.RequestDataForm;
import com.nba.common.form.ResponseDataForm;
import com.nba.common.framework.UserSessionData;
import com.nba.common.service.IService;
import com.nba.common.utils.Environment;
import com.nba.common.web.httpobjects.MsgResult;
import com.nba.common.web.utils.RequestUtils;
import com.nba.system.cache.MenuCache;
import com.nba.system.cache.RolePrivCache;
import com.nba.system.cache.SysAreaCache;
import com.nba.system.cache.SysDictCache;
import com.nba.system.cache.SysPrivCache;
import com.nba.system.cache.SysUrlCache;
import com.nba.system.cache.UrlExecSqlCache;
import com.nba.system.cache.UrlValidationCache;
import com.nba.system.cache.UserPrivCache;

@Controller
@RequestMapping(value="/auth")
public class LoginController extends BaseController{

	@Autowired
	private IJdbcDao jdbcDao;
	
	/**
	 * 登录
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/login")
	public @ResponseBody MsgResult login(HttpServletRequest request,
            HttpServletResponse response) throws CustomException, Exception {
        RequestDataForm requestDataForm = RequestUtils.getRequestDataForm(request);
        requestDataForm.setRequest(request);
        requestDataForm.setResponse(response);
        WebApplicationContext ctx = WebApplicationContextUtils
                .getWebApplicationContext(request.getSession()
                        .getServletContext());
        ResponseDataForm responseDataForm = ((IService) ctx
                .getBean("userLoginService")).service(requestDataForm);
        if(responseDataForm.getResult() == ResponseDataForm.FAULAIE || responseDataForm.getResult() == ResponseDataForm.RAND_FAULAIE) {
        	throw new CustomException(ErrorCode.NAME_OR_PSW_ERROR);
        }
        return new MsgResult();
    }
	@RequestMapping("/logout")
	@ResponseBody
	public MsgResult logout(HttpServletRequest request,HttpServletResponse response){
		request.getSession().removeAttribute(Environment.SESSION_USER_LOGIN_INFO);
		return new MsgResult();
	}
	/**
     * getUserBtn
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/getUserBtn")
    @ResponseBody
    public MsgResult getUserBtn(HttpServletRequest request,
            HttpServletResponse response) {
    	UserSessionData user = (UserSessionData) request.getSession().getAttribute(Environment.SESSION_USER_LOGIN_INFO);
        List<String> btnList =new ArrayList<String>();
        if(user!= null) {
            Map<String, List<String>> map = (Map<String, List<String>>)GlobalCache.getCache(UserPrivCache.class);
            if(map.containsKey(user.getUserId())){
                btnList =  map.get(user.getUserId());
            }
        }
        return new MsgResult(btnList);
    }
	/**
	 * 刷新缓存
	 * @param request
	 * @param response
	 * @param cacheType
	 */
	@RequestMapping(value="/refreshCache")
	@ResponseBody
    public MsgResult refreshCache(HttpServletRequest request,HttpServletResponse response,String clsName) 
    		throws ClassNotFoundException{
        if("all".equals(clsName)){
            GlobalCache.refreshCache(UrlValidationCache.class);
            GlobalCache.refreshCache(UrlExecSqlCache.class);
            GlobalCache.refreshCache(UserPrivCache.class);   
            GlobalCache.refreshCache(RolePrivCache.class); 
            GlobalCache.refreshCache(MenuCache.class); 
            GlobalCache.refreshCache(SysPrivCache.class); 
            GlobalCache.refreshCache(SysUrlCache.class); 
            GlobalCache.refreshCache(SysAreaCache.class); 
            GlobalCache.refreshCache(SysDictCache.class); 
        }else{
            if(clsName != null && !"".equals(clsName)){
                String[] cacheArr = clsName.split("\\,");
                for(String ca : cacheArr){
                	GlobalCache.refreshCache(Class.forName("com.nba.system.cache."+ca));
                    /*if("v".equals(ca)){
                        GlobalCache.refreshCache(UrlValidationCache.class);
                    }else if("e".equals(ca)){
                        GlobalCache.refreshCache(UrlExecSqlCache.class);
                    }else if("p".equals(ca)){
                        
                    }else if("s".equals(ca)){
                        
                    }else if("u".equals(ca)){
                        GlobalCache.refreshCache(UserPrivCache.class);
                    }*/
                }
            }
        }
        return new MsgResult();
    }
}
